package com.sun.httpclient;

import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;


public class HttpClientTest {
    /**
     * 使用httpclient发送get请求
     */
    @Test
    public void test01(){
        //closeableHttpClient 可关闭http客户端，相当于打开一个浏览器
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        String urlStr = "https://www.baidu.com";
        //构造httpGet请求对象
        HttpGet httpGet = new HttpGet(urlStr);
        //创建一个代理
        String ip = "222.124.170.66";
        int port = 8072;
        HttpHost proxy = new HttpHost(ip,port);
        //对每一个请求进行配置，会覆盖全局默认请求设置
        RequestConfig build = RequestConfig
                .custom()
                .setProxy(proxy)
                .setConnectTimeout(1000) //连接超时  三次握手时间  毫秒
                .setSocketTimeout(1000) //读取超时  表示从网址
                .setConnectionRequestTimeout(1000) //请求超时
                .build();
        httpGet.setConfig(build);
        //可关闭响应
        CloseableHttpResponse closeableHttpResponse = null;

        try {
            closeableHttpResponse = closeableHttpClient.execute(httpGet);
            //获取响应码
            StatusLine statusLine = closeableHttpResponse.getStatusLine();
            if (HttpStatus.SC_OK == statusLine.getStatusCode()){
                System.out.println("响应成功");
                //获取响应头
                for (Header allHeader : closeableHttpResponse.getAllHeaders()) {
                    System.out.println("响应头的名字" + allHeader.getName());
                    System.out.println("响应头的值" + allHeader.getValue());
                }

                //响应结果
                HttpEntity entity = closeableHttpResponse.getEntity();
                EntityUtils.toByteArray(entity);
                //获取ContentType
                Header contentType = entity.getContentType();

                String s = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                System.out.println(s);
                //确保  流  关闭
                EntityUtils.consume(entity);
            }else {
                System.out.println("响应失败！！！！！！");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (closeableHttpClient != null){
                try {
                    closeableHttpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (closeableHttpResponse != null){
                try {
                    closeableHttpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
